# -*- coding: utf-8 -*-

# jwt模块
import jwt
import time

# token签名秘钥
token_secret_key = "django_token_secret_key"
token_algorithm = "HS256"


# 创建token
def token_encode(username=""):
    '''
    :param username: 账户名必备
    :return: 编码好的token字符串
    '''

    encoded_token = jwt.encode({
        "username": username,
        "create_time": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    },
        token_secret_key,
        algorithm=token_algorithm
    )

    return encoded_token


# 解码token
def token_decode(token):
    decoded_token = jwt.decode(token, token_secret_key, token_algorithm)
    return decoded_token
